package com.zzj.ecology.admin.controller.system;

import com.zzj.ecology.admin.controller.BaseController;
import com.zzj.ecology.common.constant.UrlConstants;
import com.zzj.ecology.common.enums.BusinessType;
import com.zzj.ecology.framework.annotation.Log;
import com.zzj.ecology.framework.annotation.RepeatSubmit;
import com.zzj.ecology.system.bean.system.SysDept;
import com.zzj.ecology.system.dto.HttpResult;
import com.zzj.ecology.system.domain.Page;
import com.zzj.ecology.system.bean.vo.dtree.DTreeResponse;
import com.zzj.ecology.system.service.system.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName SysDeptController
 * @Description TODO 部门控制器
 * @Author zhijun zeng at 23:00 2020/4/9
 * @Version 1.0
 **/
@Api(value = "SysDept", description = "SysDept系统部门相关操作接口")
@RestController
public class SysDeptController extends BaseController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 分页查询部门数据
     *
     * @param page
     * @return
     */
    @RequestMapping(value = UrlConstants.DeptUrl.LIST)
    public HttpResult findPageList(Page page) {
        HttpResult result = sysDeptService.findPageList(page);
        return success(result);
    }

    /**
     * 查询下拉框数据
     *
     * @return
     */
    @ApiOperation(value = "查询部门下拉框DTREE数据", notes = "查询部门下拉框数据，返回DTREE数据", httpMethod = "POST")
    @RequestMapping(value = UrlConstants.DeptUrl.DTREE, method = RequestMethod.POST)
    public DTreeResponse findSelectBoxList() {
        DTreeResponse dTreeResponse = sysDeptService.dtree();
        return dTreeResponse;
    }

    @ApiOperation(value = "查询部门名称是否存在", notes = "根据 部门名称 查询部门名称是否已经存在. 存在返回 SUCCESS 字符串, 不存在返回 ERROR 字符串", httpMethod = "POST")
    @PostMapping(value = UrlConstants.DeptUrl.CHECK_EXISTS)
    public String checkExists(@RequestParam("deptName") String deptName) {
        return sysDeptService.checkExists(deptName) ? HttpResult.SUCCESS_TAG : HttpResult.FAILURE_TAG;
    }

    @ApiOperation(value = "添加一条新的部门记录", notes = "添加一条新的部门记录, 并与部门领导(用户)建立关联", httpMethod = "POST")
    @RepeatSubmit
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping(value = UrlConstants.DeptUrl.CREATE)
    public HttpResult create(SysDept sysDept) {
        if (sysDept.getParentId() == null) sysDept.setParentId(0);//默认顶级部门
        sysDept.setCreateAt(new Date());
        sysDept.setCreateBy(this.getUsername());
        HttpResult result = sysDeptService.create(sysDept);
        return success(result);
    }

    @ApiOperation(value = "修改部门信息", notes = "根据部门ID修改部门记录, 并与部门领导(用户)重新建立关联", httpMethod = "POST")
    @RepeatSubmit
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = UrlConstants.DeptUrl.UPDATE)
    public HttpResult update(SysDept sysDept) {
        if (sysDept.getParentId() == null) sysDept.setParentId(0);//默认顶级部门
        sysDept.setUpdateAt(new Date());
        sysDept.setUpdateBy(this.getUsername());
        HttpResult result = sysDeptService.update(sysDept);
        return success(result);
    }

    @ApiOperation(value = "批量删除", notes = "批量删除菜单", httpMethod = "POST")
    @RepeatSubmit
    @Log(title = "部门管理",businessType = BusinessType.DELETE)
    @PostMapping(value = UrlConstants.DeptUrl.DELETE_BY_IDS)
    public HttpResult updateDeleteStatusByIds(@RequestParam("ids") List<Integer> ids) {
        HttpResult result = HttpResult.error();
        if (ids.size() > 0) {
            result = sysDeptService.updateDeleteStatusByIds(ids, getUsername());
        }
        return success(result);
    }



}



